package org.bluedream.core.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.bluedream.comm.utils.EmptyUtil;
import org.bluedream.core.module.sys.entity.dict.DictData;
import org.bluedream.core.module.sys.service.dict.DictDataService;
import org.bluedream.core.module.sys.service.dict.DictTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @ClassName DictUtil
 * @Description TODO
 * @Author foxsand
 * @Data 2021-09-10 9:07
 * @Version
 */
public class DictUtil {
    private static Logger logger = LoggerFactory.getLogger(DictUtil.class);
    private static DictDataService dictDataService = SpringUtil.getBean(DictDataService.class);
    private static DictTypeService dictTypeService = SpringUtil.getBean(DictTypeService.class);

    private DictUtil(){
        throw new AssertionError();
    }

    public static String getDictValue(String dictType , String dictLabel , String defaultValue){
        QueryWrapper<DictData> qw = new QueryWrapper<>();
        qw.eq("dict_type" , dictType);
        qw.eq("dict_label" , dictLabel);
        qw.eq("org_code" , UserUtil.getLoginOrgCode());
        DictData dictData = dictDataService.getOne(qw);
        if (EmptyUtil.isNoEmpty(dictData)){
            return dictData.getDictValue();
        }else {
            return defaultValue;
        }
    }

    public static String getDictLabel(String dictType, String dictValue, String defaultValue){
        QueryWrapper<DictData> qw = new QueryWrapper<>();
        qw.eq("dict_type" , dictType);
        qw.eq("dict_value" , dictValue);
        qw.eq("org_code" , UserUtil.getLoginOrgCode());
        DictData dictData = dictDataService.getOne(qw);
        if (EmptyUtil.isNoEmpty(dictData)){
            return dictData.getDictLabel();
        }else {
            return defaultValue;
        }
    }

    public static List<DictData> getDictList(String dictType){
        QueryWrapper<DictData> qw = new QueryWrapper<>();
        qw.eq("dict_type" , dictType);
        qw.eq("org_code" , UserUtil.getLoginOrgCode());
        return dictDataService.list(qw);
    }
}
